﻿using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Tetris.Graphics;
using Tetris.Interfaces;
using Tetris.Levels;
using Tetris.Utility;

namespace Tetris
{
    public class ContentFactory: IContentFactory
    {
        private readonly IGameManager _gameManager;
        private SpriteFont _gameFont;
        private SpriteFont _menuFont;
        private SoundEffect _menuBackgroundMusic;
        private Texture2D _gradientTexture;
        private SoundEffect _laserSound;

        public ContentFactory( IGameManager gameManager )
        {
            _gameManager = gameManager;
        }

        public IDrawableObject Get(string identifier)
        {
            if ( identifier.Equals(Constants.Content.Levels.ONE))
            {
                Viewport viewport = _gameManager.Game.GraphicsDevice.Viewport;
                Background background = new Background(viewport.Width, viewport.Height, new[] {new StaticBackgroundLayer(
                                                                                                   Constants.Content.Graphics.MAIN_BACKGROUND)});
                Song music = _gameManager.Game.Content.Load<Song>(Constants.Content.Songs.MUSIC_LEVEL_ONE);
                return new DefaultLevel(background, music);
            }
            return null;
        }

        public SpriteFont GameFont
        {
            get
            {
                if( _gameFont == null )
                {
                    _gameFont = _gameManager.Game.Content.Load<SpriteFont>( Constants.Content.Fonts.GAME_FONT );
                }

                return _gameFont;
            }
        }

        public SpriteFont MenuFont
        {
            get
            {
                if( _menuFont == null )
                {
                    _menuFont = _gameManager.Game.Content.Load<SpriteFont>( Constants.Content.Fonts.MENU_FONT );
                }

                return _menuFont;
            }
        }

        public SoundEffect LaserSound 
        { 
            get
            {
                if (_laserSound == null)
                {
                    _laserSound =
                        _gameManager.Game.Content.Load<SoundEffect>(Constants.Content.Sounds.LASER_SOUND);
                }

                return _laserSound;
            } 
        }

        public Texture2D GradientTexture
        {
            get
            {
                if( _gradientTexture == null )
                {
                    _gradientTexture =
                        _gameManager.Game.Content.Load<Texture2D>( Constants.Content.Graphics.GRADIENT_TEXTURE );
                }

                return _gradientTexture;
            }
        }

        public Texture2D GetTexture(string identifier)
        {
            if (identifier.Equals(Constants.Content.Particles.SHIP_MOTOR_PARTICLE))
                return _gameManager.Game.Content.Load<Texture2D>(Constants.Content.Particles.SHIP_MOTOR_PARTICLE);
            if (identifier.Equals(Constants.Content.Particles.LASER_PARTICLE))
            {
                return _gameManager.Game.Content.Load<Texture2D>(Constants.Content.Particles.LASER_PARTICLE);
            }
            if (identifier.Equals(Constants.Content.Graphics.TETRIS_BLOCK))
            {
                return _gameManager.Game.Content.Load<Texture2D>(Constants.Content.Graphics.TETRIS_BLOCK);
            }
            return null;
        }

    }
}
